# demo01_subprocess.py
# 编写一个脚本，用于测试远程主机的连通性，ping3次，输出到黑洞设备
# 如果ping通，打印'xxxx:up'，否则打印'xxxx:down'
# ping('www.baidu.com')
# 定义列表，放10个主机地址，使用ping函数来测试
import subprocess
def ping(host):
    # 执行ping系统命令
    result = subprocess.run(f'ping -c 3 {host} &> /dev/null', shell=True)
    # print(result)
    if result.returncode == 0:
        print(f'{host}: up')
    else:
        print(f'{host}: down')
if __name__ == '__main__':
    # ping('192.168.88.100')
    hosts = ['192.168.88.100', 'www.baidu.com', 'www.qq.com', 'www.google.com', 'www.douyin.com', '127.0.0.1', 'www.meituan.com', 'www.sina.com.cn']
    for ip in hosts:
        ping(ip)